package com.blb.community_admin.controller;

import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.SysDept;
import com.blb.community_admin.entity.vo.DeptVO;
import com.blb.community_admin.service.ISysDeptService;
import com.blb.community_admin.util.DeptTree;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Api(tags = "部门")
@RestController
public class SysDeptController {

    @Resource
    private ISysDeptService sysDeptService;

    @ApiOperation("查询部门")
    @GetMapping("/depts/list")
    public ResponseResult<List<DeptVO>> getDepts(){
        List<DeptVO> depts = sysDeptService.getDepts();
        return ResponseResult.ok(depts);
    }
    @ApiOperation("单表查询部门")
    @GetMapping("/depts/list/chick")
    public ResponseResult<List<SysDept>> getDeptsList(){
        List<SysDept> list = sysDeptService.list(null);
        return ResponseResult.ok(list);
    }

    @ApiOperation("按条件查询部门")
    @GetMapping("/depts/getDeptsByTerm")
    public ResponseResult<List<DeptVO>> getDeptsByTerm(String deptName,String status){
        List<DeptVO> deptsByTerm = sysDeptService.getDeptsByTerm(deptName, status);
        List<DeptVO> deptVOS = new DeptTree(deptsByTerm).buildTree();
        return ResponseResult.ok(deptVOS);
    }

    @Log(module = "部门管理", operationType = 1, requestMethod = "POST")
    @ApiOperation("新增部门")
    @PostMapping("/depts/addDepts")
    public ResponseResult<String> addDepts(@RequestBody SysDept sysDept){
        sysDept.setCreateTime(LocalDateTime.now());
        sysDeptService.save(sysDept);
        return ResponseResult.ok("add");
    }

    @Log(module = "部门管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改部门")
    @PutMapping("/depts/updateDepts")
    public ResponseResult<String> updateDepts(@RequestBody SysDept sysDept){
        sysDept.setUpdateTime(LocalDateTime.now());
        sysDeptService.updateById(sysDept);
        return ResponseResult.ok("update");
    }

    @Log(module = "部门管理", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除部门")
    @DeleteMapping("/depts/deleteDepts/{deptId}")
    public ResponseResult<String> deleteDepts(@PathVariable long deptId){
        sysDeptService.removeById(deptId);
        return ResponseResult.ok("delete");
    }

    @ApiOperation("根据userId查询部门信息")
    @GetMapping("/depts/getDeptByuserId/{userId}")
    public ResponseResult<SysDept> getDeptByuserId(@PathVariable long userId){
        SysDept deptByuserId = sysDeptService.getDeptByuserId(userId);
        return ResponseResult.ok(deptByuserId);
    }


}

